
package com.wyzz.hr.bo.impl;

import java.util.*;
import org.apache.axis.utils.StringUtils;
import org.hibernate.FetchMode;
import org.hibernate.criterion.*;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.Constants;
import com.wyzz.hr.base.Status;
import com.wyzz.hr.bo.IRecruitplanBo;
import com.wyzz.hr.bo.ISysLogBO;
import com.wyzz.hr.dao.IRecruitplanDAO;
import com.wyzz.hr.dao.IStatusDAO;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.PositionBase;
import com.wyzz.hr.pojo.Recruitplan;
import com.wyzz.hr.pojo.Statusconf;
import com.wyzz.hr.pojo.StatusconfPK;
import com.wyzz.hr.pojo.base.BaseRecruitplan;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.BaseCrit;
import com.wyzz.hr.util.MyTools;
import com.wyzz.hr.util.Pager;
import com.wyzz.hr.vo.RecruitSearchBean;

public class RecruitplanBoImpl extends EmpApproverBoImpl implements
		IRecruitplanBo, Constants, Status {

	public RecruitplanBoImpl() {
	}

	public List deleteRecruitplan(String id) {
		List errors = new ArrayList();
		Recruitplan plan = (Recruitplan) dao.loadObject(
				com.wyzz.hr.pojo.Recruitplan.class, id, null, new boolean[0]);
		if (errors.size() < 1)
			dao.deleteObject(plan);
		return errors;
	}

	public List getObjects(Class clas, String fetch[]) {
		return dao.getObjects(clas, null);
	}

	public List insertRecruitplan(Recruitplan recruitplan, String empNo) {
		List errors = new ArrayList();
		try {
			recruitplan.setRecpCreateBy(new Employee(empNo));
			recruitplan.setRecpLastChangeBy(new Employee(empNo));
			recruitplan.setRecpCreateTime(new Date());
			recruitplan.setRecpLastChangeTime(new Date());
			dao.saveObject(recruitplan);
			if (recruitplan.getRecpStatus().intValue() != 2)
				addLog(recruitplan, empNo);
			return errors;
		} catch (Exception e) {
			errors.add("\u6DFB\u52A0\u62DB\u8058\u65B9\u6848\u5931\u8D25!");
		}
		return errors;
	}

	public Recruitplan loadRecruitplan(String id, String fetch[]) {
		return (Recruitplan) dao.loadObject(com.wyzz.hr.pojo.Recruitplan.class,
				id, fetch, new boolean[0]);
	}

	public List findRecruitplanByIds(String ids[], String fetch[]) {
		List result;
		try {
			DetachedCriteria dc = DetachedCriteria
					.forClass(com.wyzz.hr.pojo.Recruitplan.class);
			for (int i = 0; i < fetch.length; i++)
				dc.setFetchMode(fetch[i], FetchMode.JOIN);

			BaseCrit.addDC(dc, Recruitplan.PROP_ID, "in", ids);
			dc.addOrder(Order.asc(Recruitplan.PROP_RECP_NO));
			result = dao.findByCriteria(dc);
			if (result.isEmpty())
				return null;
		} catch (Exception e) {
			return null;
		}
		return result;
	}

	public List searchRecruitplan(DetachedCriteria dc,
			RecruitSearchBean searchBean, Pager page) {
		dc.createAlias(Recruitplan.PROP_RECP_DEPARTMENT_NO, "recpDepartmentNo",
				1);
		dc.createAlias(Recruitplan.PROP_RECP_TYPE, "recpType", 1);
		dc.createAlias(Recruitplan.PROP_RECP_WORK_LOCATION, "recpWorkLocation",
				1);
		dc.createAlias(Recruitplan.PROP_RECP_PB_NO, "recpPbNo", 1);
		if (searchBean.getRecpNo() != null)
			dc.add(Restrictions.eq(Recruitplan.PROP_RECP_NO, searchBean
					.getRecpNo()));
		if (searchBean.getEmpdept() != null
				&& searchBean.getEmpdept().getEmpDeptNo() != null)
			BaseCrit.addDeptDC(dc, Recruitplan.PROP_RECP_DEPARTMENT_NO,
					Recruitplan.PROP_RECP_PB_NO, Integer.valueOf(1), searchBean
							.getEmpdept().getEmpDeptNo());
		if (!StringUtils.isEmpty(searchBean.getRecpWorkLocation()))
			dc.add(Restrictions.eq((new StringBuilder()).append(
					Recruitplan.PROP_RECP_WORK_LOCATION).append(".").append(
					"id").toString(), searchBean.getRecpWorkLocation()));
		if (!StringUtils.isEmpty(searchBean.getEmpType()))
			dc.add(Restrictions.eq((new StringBuilder()).append(
					Recruitplan.PROP_RECP_TYPE).append(".").append("id")
					.toString(), searchBean.getEmpType()));
		if (searchBean.getRecpStatus() != null)
			dc.add(Restrictions.eq(Recruitplan.PROP_RECP_STATUS, searchBean
					.getRecpStatus()));
		String alias[] = { Recruitplan.PROP_RECP_START_DATE,
				Recruitplan.PROP_RECP_END_DATE };
		BaseCrit.addDCTwoDateRange(dc, alias, searchBean.getStartDate(),
				searchBean.getEndDate());
		BaseAction.addOrders(dc, page, new String[] { (new StringBuilder())
				.append(Recruitplan.PROP_RECP_NO).append("-down").toString() });
		page.splitPage(dc);
		return dao
				.findByCriteria(dc, page.getPageSize(), page.getCurrentPage());
	}

	public List updateRecruitplan(Recruitplan plan, String empNo) {
		List errors = new ArrayList();
		try {
			plan.setRecpLastChangeBy(new Employee(empNo));
			plan.setRecpLastChangeTime(new Date());
			dao.saveOrupdate(plan);
			return errors;
		} catch (Exception e) {
			errors
					.add("\u66F4\u65B0\u62DB\u8058\u65B9\u6848\u5931\u8D25\uFF01");
		}
		return errors;
	}

	public boolean updateRecruitplan(Integer status, String id,
			String comments, String empNo) {
		try {
			String commSql = "";
			if (comments != null && comments.trim().length() > 0)
				commSql = (new StringBuilder()).append(", recpComments='")
						.append(comments.trim()).append("' ").toString();
			if (empNo != null && empNo.trim().length() > 0)
				commSql = (new StringBuilder()).append(commSql).append(
						", recpLastChangeBy='").append(empNo.trim()).append(
						"' ").toString();
			commSql = (new StringBuilder()).append(commSql).append(
					", recpLastChangeTime=current_timestamp").toString();
			dao.exeHql((new StringBuilder()).append(
					" update Recruitplan set recpStatus=").append(
					String.valueOf(status)).append(commSql).append(
					" where id ='").append(id).append("'").toString());
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public String findStatusByRecpStatusNo(int no) {
		if (no >= 0)
			try {
				Statusconf stconf = (Statusconf) dao.loadObject(
						com.wyzz.hr.pojo.Statusconf.class, new StatusconfPK(
								Integer.valueOf(no), "recruitplan"), null,
						new boolean[0]);
				if (stconf != null)
					return stconf.getStatusconfDesc();
			} catch (Exception e) {
				return "\u65E0\u72B6\u6001";
			}
		return "\u65E0\u72B6\u6001";
	}

	public List getRecruitplanStatus() {
		return statusDAO.getStatusByTable("recruitplan");
	}

	public IRecruitplanDAO getDao() {
		return dao;
	}

	public void setDao(IRecruitplanDAO dao) {
		this.dao = dao;
	}

	public IStatusDAO getStatusDAO() {
		return statusDAO;
	}

	public void setStatusDAO(IStatusDAO statusDAO) {
		this.statusDAO = statusDAO;
	}

	public boolean loadCompaplanByEmpNo(String empNo) {
		return false;
	}

	public void updateRecruitplanStatusAsSubmit(String changIds, String empNo,
			int status) {
		dao.exeHql((new StringBuilder()).append(
				" update Recruitplan set recpStatus=").append(status).append(
				",recpLastChangeTime=current_timestamp").append(" where id ='")
				.append(changIds).append("'").toString());
		if (changIds != null) {
			Recruitplan recruitplan = (Recruitplan) dao.loadObject(
					com.wyzz.hr.pojo.Recruitplan.class, changIds, null,
					new boolean[0]);
			addLog(recruitplan, empNo);
		}
	}

	public void addLog(Recruitplan recruitplan, String empNo) {
		ISysLogBO logBO = (ISysLogBO) SpringBeanFactory.getBean("logBO");
		String tempText = "";
		switch (recruitplan.getRecpStatus().intValue()) {
		case 2: // '\002'
			tempText = "\u63D0\u4EA4\u7533\u8BF7";
			break;

		case 5: // '\005'
			tempText = "\u6B63\u5728\u5BA1\u6279";
			break;

		case 15: // '\017'
			tempText = "HR\u5907\u6848";
			break;

		case 21: // '\025'
			tempText = "\u62D2\u7EDD";
			break;

		case 22: // '\026'
			tempText = "\u4F5C\u5E9F";
			break;
		}
		try {
			logBO.addToSyslog("recruitplan", empNo, recruitplan
					.getRecpCreateBy().getId(), recruitplan.getId().toString(),
					0, tempText, recruitplan.getRecpComments());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String updateRecruitplanAsComment(String id, String recpComments,
			Integer status) {
		Recruitplan rp;
		rp = (Recruitplan) dao.loadObject(com.wyzz.hr.pojo.Recruitplan.class,
				id, null, new boolean[0]);
		if (rp == null)
			return "ERROR:\u6B64\u62DB\u8058\u8BA1\u5212\u5728\u6570\u636E\u5E93\u4E2D\u4E0D\u5B58\u5728\uFF01";
		try {
			if (rp.getRecpStatus().intValue() != 21
					&& rp.getRecpStatus().intValue() != 5)
				return "ERROR:\u62DB\u8058\u8BA1\u5212\u72B6\u6001\u4E0D\u662F\u8349\u7A3F\u6216\u5DF2\u62D2\u7EDD\uFF0C\u65E0\u6CD5\u63D0\u4EA4";
		} catch (Exception e) {
			e.printStackTrace();
		}
		dao.exeHql((new StringBuilder()).append(
				" update Recruitplan set recpComments='").append(recpComments)
				.append("',recpStatus='").append(status)
				.append("' where id ='").append(id).append("'").toString());
		return null;
	}

	public List searchRecruitplanforHR(DetachedCriteria dc, Pager page) {
		BaseAction.addOrders(dc, page, new String[] { (new StringBuilder())
				.append(Recruitplan.PROP_RECP_NO).append("-down").toString() });
		page.splitPage(dc);
		return dao
				.findByCriteria(dc, page.getPageSize(), page.getCurrentPage());
	}

	public List loadRecruitplanByDC(DetachedCriteria dc) {
		return dao.findByCriteria(dc);
	}

	public List update_closeRecruitplan(String id, String empNo) {
		ISysLogBO logBO = (ISysLogBO) SpringBeanFactory.getBean("logBO");
		List errors = new ArrayList();
		String fetch[] = { "recpCreateBy" };
		Recruitplan plan = (Recruitplan) dao.loadObject(
				com.wyzz.hr.pojo.Recruitplan.class, id, fetch, new boolean[0]);
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Recruitapplier.class);
		detachedCriteria.setFetchMode("recaPlan", FetchMode.JOIN);
		detachedCriteria.add(Restrictions.eq("recaPlan.id", plan.getId()));
		detachedCriteria
				.add(Restrictions.eq("recaStatus", Integer.valueOf(13)));
		int factNum = dao.findRowCountByCriteria(detachedCriteria);
		String timeSql = "', recp_end_date=current_timestamp ";
		if (factNum != 0)
			;
		if (factNum <= 0 || factNum >= plan.getRecpNumberExpect().intValue())
			;
		if (factNum < plan.getRecpNumberExpect().intValue())
			;
		addLog(plan, empNo);
		return errors;
	}

	public List getApprovedPlanList() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Recruitplan.class);
		String fetch[] = { RP_DEPT_NO, RP_CREATE_BY, RP_LAST_CHANGE_BY,
				RP_TYPE, RP_WORKLOCATION, RP_POSITION };
		for (int i = 0; i < fetch.length; i++)
			dc.setFetchMode(fetch[i], FetchMode.JOIN);

		dc.add(Restrictions.eq("recpStatus", Integer.valueOf(16)));
		return dao.findByCriteria(dc);
	}

	public void updatePlan(Recruitplan plan) {
		dao.updateObject(plan);
	}

	public List searchApprovePlanWithCondition(String condition) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Recruitplan.class);
		dc.createAlias(RP_DEPT_NO, "recpDepartmentNo", 1);
		dc.createAlias(RP_TYPE, "recpType", 1);
		dc.createAlias(RP_WORKLOCATION, "recpWorkLocation", 1);
		dc.createAlias(RP_POSITION, "recpPbNo", 1);
		dc.createAlias(RP_CREATE_BY, "recpCreateBy", 1);
		dc.createAlias((new StringBuilder()).append(RP_CREATE_BY).append(".")
				.append(Employee.PROP_EMP_DEPT_NO).toString(),
				(new StringBuilder()).append(RP_CREATE_BY).append(".").append(
						Employee.PROP_EMP_DEPT_NO).toString(), 1);
		dc.createAlias((new StringBuilder()).append(RP_CREATE_BY).append(".")
				.append(Employee.PROP_EMP_LOCATION_NO).toString(),
				(new StringBuilder()).append(RP_CREATE_BY).append(".").append(
						Employee.PROP_EMP_LOCATION_NO).toString(), 1);
		dc.createAlias(RP_LAST_CHANGE_BY, "recpLastChangeBy", 1);
		dc.createAlias((new StringBuilder()).append(RP_LAST_CHANGE_BY).append(
				".").append(Employee.PROP_EMP_DEPT_NO).toString(),
				(new StringBuilder()).append(RP_LAST_CHANGE_BY).append(".")
						.append(Employee.PROP_EMP_DEPT_NO).toString(), 1);
		dc.createAlias((new StringBuilder()).append(RP_LAST_CHANGE_BY).append(
				".").append(Employee.PROP_EMP_LOCATION_NO).toString(),
				(new StringBuilder()).append(RP_LAST_CHANGE_BY).append(".")
						.append(Employee.PROP_EMP_LOCATION_NO).toString(), 1);
		dc.add(Restrictions.in(Recruitplan.PROP_RECP_STATUS, Status.PROCESSED));
		Date nowTime = new Date();
		org.hibernate.criterion.Criterion tempC = Restrictions
				.or(
						Restrictions
								.and(
										Restrictions
												.and(
														Restrictions
																.isNotNull(Recruitplan.PROP_RECP_START_DATE),
														Restrictions
																.isNotNull(Recruitplan.PROP_RECP_END_DATE)),
										Restrictions
												.and(
														Restrictions
																.le(
																		Recruitplan.PROP_RECP_START_DATE,
																		nowTime),
														Restrictions
																.ge(
																		Recruitplan.PROP_RECP_END_DATE,
																		nowTime))),
						Restrictions
								.and(
										Restrictions
												.and(
														Restrictions
																.isNotNull(Recruitplan.PROP_RECP_START_DATE),
														Restrictions
																.isNull(Recruitplan.PROP_RECP_END_DATE)),
										Restrictions
												.le(
														Recruitplan.PROP_RECP_START_DATE,
														nowTime)));
		dc.add(tempC);
		if (!StringUtils.isEmpty(condition))
			try {
				Integer conditionStatus = Integer.valueOf(MyTools.parseNumber(
						condition, java.lang.Integer.class).intValue());
				BaseCrit.addDC(dc, Recruitplan.PROP_RECP_NO, "eq",
						new Integer[] { conditionStatus });
			} catch (Exception e) {
				org.hibernate.criterion.Criterion tempCirt = Restrictions.or(
						Restrictions.ilike((new StringBuilder()).append(
								RP_DEPT_NO).append(".").append(
								Department.PROP_DEPARTMENT_NAME).toString(),
								(new StringBuilder()).append("%").append(
										condition).append("%").toString()),
						Restrictions.ilike((new StringBuilder()).append(
								RP_POSITION).append(".").append(
								PositionBase.PROP_PB_NAME).toString(),
								(new StringBuilder()).append("%").append(
										condition).append("%").toString()));
				dc.add(tempCirt);
			}
		return dao.findByCriteria(dc);
	}

	private static final String RP_LAST_CHANGE_BY;
	private static final String RP_CREATE_BY;
	private static final String RP_DEPT_NO;
	private static final String RP_TYPE;
	private static final String RP_WORKLOCATION;
	private static final String RP_POSITION;
	private IRecruitplanDAO dao;
	private IStatusDAO statusDAO;

	static {
		RP_LAST_CHANGE_BY = BaseRecruitplan.PROP_RECP_LAST_CHANGE_BY;
		RP_CREATE_BY = BaseRecruitplan.PROP_RECP_CREATE_BY;
		RP_DEPT_NO = BaseRecruitplan.PROP_RECP_DEPARTMENT_NO;
		RP_TYPE = BaseRecruitplan.PROP_RECP_TYPE;
		RP_WORKLOCATION = BaseRecruitplan.PROP_RECP_WORK_LOCATION;
		RP_POSITION = BaseRecruitplan.PROP_RECP_PB_NO;
	}
}
